package com.alix.paymoney.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alix.paymoney.R;
import com.alix.paymoney.base.BaseFragment;
import com.alix.paymoney.base.ChannelAdapter;
import com.alix.paymoney.entity.Channel;
import com.alix.paymoney.presenter.ChannelPresenter;
import com.alix.paymoney.presenter.impl.ChannelPresenterImpl;
import com.alix.paymoney.view.ChannelListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements ChannelListView {


    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refresh_view)
    SwipeRefreshLayout refreshView;
    List<Channel> channelList = new ArrayList<>();
    private ChannelPresenter channelPresenter;
    ChannelAdapter channelAdapter;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public int getLayout() {
        return R.layout.fragment_home;
    }

    SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            channelPresenter.getChannelList("1");
        }
    };

    @Override
    public void afterBindView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        channelAdapter = new ChannelAdapter(getContext(), channelList);
        recyclerView.setAdapter(channelAdapter);
        channelPresenter = new ChannelPresenterImpl(this);
        refreshView.setOnRefreshListener(onRefreshListener);
        refreshView.setColorSchemeColors(ContextCompat.getColor(getContext(), R.color.red));
        refreshView.setRefreshing(true);
        onRefreshListener.onRefresh();


    }

    @Override
    public void showTips(String msg) {

        showSnackBar(recyclerView, msg);

    }

    @Override
    public void stopSwipRefresh() {
        stopRefresh(refreshView);
    }

    @Override
    public void getChannelSuccess(List<Channel> list) {
        channelList.clear();
        if (list != null) {
            channelList.addAll(list);
        }
        channelAdapter.notifyDataSetChanged();
    }

    @Override
    public void getChannelError(int errorCode, String errorInfo) {

    }


}
